import { Component, OnInit } from '@angular/core';
import { SafenlService } from '../service/safenl.service';
import {Consistent} from "../entity/Consistent"
import { TextService } from "../service/text.service";
import { SMTRes } from '../entity/SMTRes';
import { minunsatRes } from '../entity/minunsatRes';

@Component({
  selector: 'app-errorbar',
  templateUrl: './errorbar.component.html',
  styleUrls: ['./errorbar.component.css']
})
export class ErrorbarComponent implements OnInit {

  constructor(
    private safenlService: SafenlService,
    private textService: TextService,
  ) {
    this.safenlService.LayerEmmited$.subscribe(Layer => {
      console.log(Layer);
      this.safenlService.ConsistentEmmited$.subscribe(Consistents => {
        this.Consistents = Consistents;
        this.smtreslist = null;
        this.consist = null;
        if(Consistents.length == 0){
          this.checkres = Layer + " has no error!"
          document.getElementById("errorList").style.display = 'none';
          // console.log(this.checkres);
        }
        else{
          this.checkres = null
          document.getElementById("errorList").style.display = 'block';
          this.stage = Consistents[0].name
          // console.log(this.stage)
        }
        document.getElementById("consist").style.display = 'none';
        // for(let consistent of Consistents){
        //   for(let safenl of consistent.safenlList){
        //     console.log(safenl);
        //   }
        // }
      })
    })

    this.safenlService.SMTEmmited$.subscribe(results => {
      this.checkres = null;
      this.Consistents = []
      this.consist = null;
      document.getElementById("errorList").style.display = 'none';
      document.getElementById("consist").style.display = 'none';
      this.smtreslist = results;
      // if(SMTRES){
      //   this.smtres = "Sat";
      // }else{
      //   this.smtres = "Unsat";
      // }
    })

    this.safenlService.consistEmit$.subscribe(results => {
      this.Consistents = []
      this.smtreslist = null;
      document.getElementById("errorList").style.display = 'none';
      document.getElementById("smtRes").style.display = 'none';
      document.getElementById("consist").style.display = 'block';
      if(this.consist != null && document.getElementById(this.consist.name + "-P") != null) document.getElementById(this.consist.name + "-P").style.display = 'none';
      this.consist = results;
      console.log("results type ->", typeof results)
      console.log('results ->', results)
      this.minunsatRes = this.consist.inconsistentType
      console.log(Object.keys(this.minunsatRes)[0])
      console.log(Object.values(this.minunsatRes)[0])
      this.consist.inconsisTypeName = Object.keys(this.minunsatRes)[0].split("::")[0]
      this.consist.elapse = Object.keys(this.minunsatRes)[0].split("::")[1]
      this.consist.inconsisSafeNL = Object.values(this.minunsatRes)[0]
    })
   }

  ngOnInit() {
  }

  Consistents: Consistent[];
  location: string;
  smtreslist: SMTRes[];
  consist: minunsatRes;
  minunsatRes: Object;
  checkres: string;
  stage: string;
  
  locate(safenl: string){
    this.location = safenl;
    let position = safenl.substring(6,safenl.indexOf("]"));
    // console.log(position);
    let pos = {
      lineNumber: Number(position),
      column: Number(position)
    }
    this.textService.editor.setPosition(pos);
    this.textService.editor.revealPositionInCenter(pos, 1 /* Immediate */);
  }

  change(title: string){
    let tabList: string[];
    console.log(title);
    if(this.Consistents != null && this.Consistents.length>0){
      for(let consistent of this.Consistents){
        if(consistent.name != title){
          document.getElementById(consistent.name + "-P").style.display = 'none';
        }else{
          console.log(consistent.safenlList);
          document.getElementById(title + "-P").style.display = 'block';
        }
      }
    }
    
    if(this.smtreslist != null && this.smtreslist.length>0){
      for(let smtres of this.smtreslist){
        if(smtres.name != title){
          document.getElementById(smtres.name + "-P").style.display = 'none';
        }else{
          document.getElementById(title + "-P").style.display = 'block';
        }
      }
    }

    if(title == "consisnt_check_result" && this.consist != null ){
      console.log(111)
      document.getElementById(this.consist.name + "-P").style.display = 'block';
    }



    // let parTab = document.getElementById(title).parentElement;
    // var tabs = parTab.children;    
    // for(var i = 0; i <= tabs.length; i ++){
    //   if(tabs[i] != undefined){
    //     var ctabs = tabs[i].children;
    //     for(var j = 0; j < ctabs.length; j ++){
    //       console.log(ctabs[j]);
    //       var id = ctabs[j].getAttribute('id');
    //       if(id != title){
    //           document.getElementById(id + "-P").style.display = 'none';
    //       }else{
    //         document.getElementById(id + "-P").style.display = 'block';
    //       }
    //     }
    //   }
    // }
  }
}
